/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tabelmodel;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.Pegawai;

/**
 *
 * @author tashiba
 */
public class PegawaiTableModel extends AbstractTableModel {

    private List<Pegawai> peg = new ArrayList<Pegawai>();

    public PegawaiTableModel(List<Pegawai> pegawai) {
        this.peg = pegawai;
    }

    public void deletePegawai(int row) {
        peg.remove(row);
        fireTableRowsDeleted(row, row);
    }

    public void updatePegawai(int row, Pegawai pegawai) {
        peg.set(row, pegawai);
        fireTableRowsUpdated(row, row);
    }

    public void addPegawai(Pegawai pegawai) {
        peg.add(pegawai);
        fireTableRowsInserted(getRowCount() - 1, getColumnCount() - 1);
    }

    @Override
    public int getRowCount() {
        return peg.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Pegawai b = peg.get(rowIndex);
        switch (columnIndex) {
             case 0:
                return b.getIdPegawai();
            case 1:
                return b.getNamaPegawai();
            case 2:
                return b.getDivisi().getIdDivisi() + " " + b.getDivisi().getNamaDivisi();
            case 3:
                return b.getUsername();
            case 4:
                return b.getPassword();

            default:
                return "";
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
             case 0:
                return "Id Pegawai";
            case 1:
                return "Nama Pegawai";
            case 2:
                return "Divisi";
            case 3:
                return "Username";
            case 4:
                return "Password";
            default:
                return "";
        }
    }
}

